﻿using KouDai.WinPad.Models;
using KouDai.WinPad.ViewModels;
using System;
using System.Collections.Generic;
using Windows.Foundation;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace KouDai.WinPad.Views
{
    public sealed partial class SecondHotPage : KouDai.WinPad.Common.LayoutAwarePage
    {
        private SecondHotPageViewModel _viewModel;

        public SecondHotPage()
        {
            this.InitializeComponent();
        }
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            _viewModel = this.DataContext as SecondHotPageViewModel;
            zoomOutGridView.ItemsSource = this.itemsViewSource.View.CollectionGroups;
        }

        public static Rect GetElementRect(FrameworkElement element)
        {
            GeneralTransform buttonTransform = element.TransformToVisual(null);
            Point point = buttonTransform.TransformPoint(new Point());
            return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
        }

        private async void Header_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            PopupMenu menu = new PopupMenu();

            foreach (HotEntity model in _viewModel.FatherHotItems)
            {
                if (!_viewModel.CurrentHotItem.OriginalAppName.Equals(model.OriginalAppName))
                {
                    menu.Commands.Add(new UICommand("         " + model.OriginalAppName + "      ", null));
                }
            }
            menu.Commands.Add(new UICommandSeparator());
            menu.Commands.Add(new UICommand("         " + _viewModel.CurrentHotItem.OriginalAppName + "      ", null));
            var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));

            if (chosenCommand != null)
            {
                if (!_viewModel.CurrentHotItem.OriginalAppName.Equals(chosenCommand.Label.Trim().ToString()))
                {
                    _viewModel.SameLevelNavigate(chosenCommand.Label);
                }
            }
        }

    }
}
